package main;

import exception.ServiceException;
import pojo.Category;
import pojo.Profile;
import pojo.Result;
import pojo.Tif;
import util.ExcelUtil;
import util.POIUtil;
import util.TifUtil;

import java.util.*;

/**
 * Created by Administrator on 2016/10/5.
 */
public class Main {
    private static String categoryFile = "E:\\zone\\migrate\\excel\\案卷目录（进馆档案）.xls";
    private static String profileFile = "E:\\zone\\migrate\\excel\\卷内目录（进馆档案）.xls";
    private static String tifPath = "C:\\Users\\Administrator\\Desktop\\jmj\\进馆扫描";
    private static String fileTargetFile = "E:\\zone\\migrate\\excel\\file.xls";
    private static String detailTargetFile = "E:\\zone\\migrate\\excel\\detail.xls";
    public static void main(String[] args){
        if(false){
            categoryFile = args[0];
            profileFile = args[1];
            tifPath = args[2];
            fileTargetFile = args[3];
            detailTargetFile = args[4];
        }

        List<Category> categoryList = ExcelUtil.getCategoryExcel(categoryFile);
        POIUtil.write(categoryList,fileTargetFile);

        Map<String,Category> categoryMap = ExcelUtil.getCategoryInfo(categoryFile);
        Map<String, List<Profile>> profileMap = ExcelUtil.getProfileInfo(profileFile);

        List<Result> result = new ArrayList<Result>();
        String categoryNo;
        int profileOrder;
        String profileName;
        String profileNo;
        Map<String,String> profileNoMap;
        int page;
        String display;
        String startIndex;
        String endIndex;
        String remark;
        String categoryName;
        List<Profile> profileList;

        for(Map.Entry<String,Category> categoryEntry : categoryMap.entrySet()){
            categoryNo = categoryEntry.getKey();
            categoryName = categoryEntry.getValue().getCategoryName();
            profileList = profileMap.get(categoryNo);
            check(categoryNo,profileList);
            for(Profile profile:profileList){
                profileNo = profile.getProfileNo();
                profileOrder = Integer.parseInt(profile.getOrderNo());
                profileName = profile.getProfileName();
                page = Integer.parseInt(profile.getPage());
                display = profile.getDisplay();
                startIndex = profile.getStartIndex();
                endIndex = profile.getEndIndex();
                remark = profile.getRemark();
                int childOrder = Integer.parseInt(categoryNo.substring(categoryNo.indexOf("/") + 1));
                String link = tifPath+"\\" + childOrder + "\\" + childOrder + "_" +
                        Integer.parseInt(profile.getStartIndex()) + ".tif";
                profileNoMap = new HashMap<String, String>();
                profileNoMap.put("value",profileNo);
                profileNoMap.put("link",link);
                result.add(new Result(categoryNo,profileOrder,profileName,profileNoMap,page,display,startIndex,endIndex,
                        remark,categoryName,""));
            }
        }

        Collections.sort(result,new Comparator<Result>() {
            @Override
            public int compare(Result o1, Result o2) {
//                int t = o1.getParentOrder()-o2.getParentOrder();
//                if(t > 0) return 1;
//                if(t < 0) return -1;

                int q = o1.getChildOrder()-o2.getChildOrder();
                if(q > 0) return 1;
                if(q < 0) return -1;

                int r = o1.getProfileOrder()-o2.getProfileOrder();
                if(r > 0) return 1;
                if(r < 0) return -1;

                return 0;
            }
        });

        POIUtil.write(result,detailTargetFile);


    }


    private static void check(String categoryNo,List<Profile> profileList) throws ServiceException{
        Profile profile;

        int page = 0;
        int startIndex=0;
        int endIndex=0;
        String lastEndIndex = "";
        for(int i=0;i<profileList.size();i++){
            profile = profileList.get(i);
            //1、页数 转换
            try {
                page = Integer.parseInt(profile.getPage());
            }catch (Exception e){
                throw new ServiceException(categoryNo + " / "+profile.getProfileNo() + " 页数转换成数字发生错误："
                        + profile.getPage() );
            }

            //2、页号匹配
            try{
                startIndex = Integer.parseInt(profile.getStartIndex());
                endIndex = Integer.parseInt(profile.getEndIndex());
                if(page-1 != endIndex-startIndex ){
                    int temp = 0/0;
                }
            }catch (Exception e){
                throw new ServiceException(categoryNo + " / "+profile.getProfileNo() + " 页号匹配发生错误："
                        + "（页数："+profile.getPage() + "），（页号-起："  +profile.getStartIndex()
                        + "），（页号-止："  +profile.getEndIndex() +"）" );

            }

            //3、上一页与下一页匹配
            try {
                if(i > 0){
                    lastEndIndex = profileList.get(i - 1).getEndIndex();
                }
                if (i > 0 && (startIndex - Integer.parseInt(lastEndIndex)) != 1) {
                    int temp = 0/0;
                }
            }catch (Exception e){
                throw new ServiceException(categoryNo + " / "+profile.getProfileNo() + " 页号起止数字不匹配："
                        + "（页号-起："  + startIndex
                        + "），（上一页号-止："  +lastEndIndex +"）" );
            }

            //5、获取tif文件夹编号
            int childOrder = 0;
            try{
                childOrder = Integer.parseInt(categoryNo.substring(categoryNo.indexOf("/") + 1));
            }catch (Exception e){
                throw new ServiceException(categoryNo + " / "+profile.getProfileNo() + " Tif文件夹编号转换错误" );
            }

            //6、tif 匹配
            try {
                Tif tif = TifUtil.getTifIno(tifPath+"\\" + childOrder + "\\" + childOrder + "_" + startIndex + ".tif");
                if (tif.getPage() != page || !"300 dots per inch".equals(tif.getX())
                        ||!"300 dots per inch".equals(tif.getY())) {
                        //||!"T6/Group 4 Fax".equals(tif.getCompress())
                    int temp = 0/0;
                }
            }catch (Exception e){
                throw new ServiceException(categoryNo + " / "+profile.getProfileNo() + " TIF文件页数错误或分辨率或压缩错误" );
            }
        }
    }
}
